home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / lm / heldLockEntry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  6.2 KB  |  244 lines

  1. /*
  2.  *   $RCSfile: heldLockEntry.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:51 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "pool.h"
  45. #include "tid.h"
  46. #include "io.h"
  47. #include "lock.h"
  48. #include "object.h"
  49. #include "msgdefs.h"
  50. #include "thread.h"
  51. #include "semaphore.h"
  52. #include "link.h"
  53. #include "lsn.h"
  54. #include "latch.h"
  55. #include "bf.h"
  56. #include "volume.h"
  57. #include "trans.h"
  58. #include "threadstate.h"
  59. #include "thread_funcs.h"
  60. #include "lm_intfuncs.h"
  61. #include "lm_extfuncs.h"
  62. #include "lock_globals.h"
  63. #include "thread_globals.h"
  64.  
  65.  
  66. static LIST AgeList;
  67.  
  68. #define LOCK_WAIT_UNIT    10 /* seconds */
  69.  
  70.  void
  71. ageLocks()
  72. {
  73.     LOCKENTRY *lockEntry, *next;
  74.  
  75.     initializeList( &AgeList );
  76.     /* Sleep right away, after startup, so the rest of 
  77.      * initializeServer() gets done.
  78.      */
  79.  
  80.     for(;;) {
  81.         /* every minute, age the locks on the list by one minute */
  82.         TRPRINT(TR_LOCK, TR_LEVEL_1, ("ageLocks sleeps for %d sec", 
  83.             LOCK_WAIT_UNIT));
  84.  
  85.         Sleep(LOCK_WAIT_UNIT);
  86.         TRPRINT(TR_LOCK, TR_LEVEL_1, ("ageLocks wakes up"));
  87.  
  88.         lockEntry = (LOCKENTRY *) FIRST_LIST_ELEMENT( &AgeList );
  89.  
  90.         if(LIST_EMPTY( &AgeList)) {
  91.             TRPRINT(TR_LOCK, TR_LEVEL_1, ("AgeList EMPTY"));
  92.         } else {
  93.             TRPRINT(TR_LOCK, TR_LEVEL_1, ("lock-aging tick"));
  94.         }
  95.         while(lockEntry != NULL) {
  96.             TRPRINT(TR_LOCK, TR_LEVEL_1, 
  97.                 ("lock request at age %d", lockEntry->time2wakeup));
  98.             next = (LOCKENTRY *) NEXT_LIST_ELEMENT( &(lockEntry->ageList));
  99.             if(lockEntry->flags & LOCK_GRANTED) {
  100.                 /* skip it - just let it be removed from the list */
  101.             } else if(--(lockEntry->time2wakeup) <= 0) {
  102.  
  103.                 TRPRINT(TR_LOCK, TR_LEVEL_1, 
  104.                     ("lock request %x, flags 0x%x, hdr 0x%x dies of old age", 
  105.                         lockEntry, lockEntry->flags, lockEntry->lockHeader));
  106.                 /* NB: there is exactly ONE element on the threadList! */
  107.                 SM_ASSERT(LEVEL_1, 
  108.                     (LIST_NOT_EMPTY( &(lockEntry->threadList) )));    
  109.  
  110.                 if(lockEntry->flags & LOCK_UPGRADE) {
  111.                     LOCKENTRY *master = (LOCKENTRY *)(lockEntry->lockHeader);
  112.                     SM_ASSERT(LEVEL_1, 
  113.                         (master ->flags & LOCK_UPGRADE_PENDING));
  114.                     master->flags &= ~LOCK_UPGRADE_PENDING;
  115.                 }
  116.                 freeLockEntry(lockEntry, esmLOCKBUSY);
  117.             }
  118.             lockEntry = next;
  119.         }
  120.     }
  121. }
  122.  
  123. int
  124. awaitLock(int lock_wait_units, LOCKENTRY *lockEntry, int state)
  125. {
  126.     lockEntry->time2wakeup = lock_wait_units;
  127.     listEnq(&AgeList, &(lockEntry->ageList));
  128.     TRPRINT(TR_LOCK, TR_LEVEL_1, ("lock %x waits, age %d", lockEntry, 
  129.         lockEntry->time2wakeup));
  130.     /*
  131.      *    put the thread on the wait list for this lock
  132.      */
  133.     return  waitList(&(lockEntry->threadList), THREAD_LOCK_WAIT);
  134. }
  135.  
  136.  
  137.  LOCKENTRY
  138. *heldLockEntry (
  139.  
  140.     register TRANSREC    *transRec,
  141.     LOCKHEADER            *lockHeader,
  142.     LOCKMODE            requestMode,
  143.     FLAGS                flags 
  144. )
  145. {
  146.  
  147.     register LOCKENTRY    *lockEntry;
  148.  
  149.  
  150.     TRPRINT(TR_LOCK, TR_LEVEL_1, ("supremum:%s requestMode:%s",
  151.             GETMODE(lockHeader->supremum), GETMODE(requestMode)));
  152.  
  153.     /*
  154.      *    check to see if the lock can be granted
  155.      */
  156.     if ((LM_Compat[lockHeader->supremum][requestMode]) &&
  157.         (LIST_EMPTY( &(lockHeader->waitList) )) &&
  158.         (LIST_EMPTY( &(lockHeader->upgradeList) )))    {
  159.  
  160.         TRPRINT(TR_LOCK, TR_LEVEL_1, ("lock request compatible"));
  161.  
  162.         /*
  163.          *    Get an new lock entry record
  164.          */
  165.         if ((lockEntry = 
  166.             newLockEntry(transRec, lockHeader, GRANT_LIST, requestMode))==NULL){
  167.             return(NULL);
  168.         }
  169.  
  170.         /*
  171.          *    check to see if we have a new supremum
  172.          */
  173.         if (!LM_Compat_Upgrade[requestMode][lockHeader->supremum])    {
  174.             
  175.             lockHeader->supremum = LM_Supremum[requestMode][lockHeader->supremum];
  176.         }
  177.         /*
  178.          *    initialize lock header information
  179.          */
  180.         lockHeader->modeCount[requestMode]++;
  181.         lockHeader->lockCount++;
  182.  
  183.         /*
  184.          *    return a pointer to the lock entry
  185.          */
  186.         return(lockEntry);
  187.  
  188.     } else {
  189.  
  190.         TRPRINT(TR_LOCK, TR_LEVEL_1, ("request mode is not compatible"));
  191.  
  192.         /*
  193.          *    check to see if the flags are instant or nowait
  194.          */
  195.         if ((flags & LOCK_NOWAIT) || (flags & LOCK_INSTANT))    {
  196.  
  197.             SM_ERROR(TYPE_USER, esmLOCKBUSY);
  198.             return(NULL);
  199.         }
  200.  
  201.         /*
  202.          *    check the deadlock
  203.          */
  204.         if (checkDeadlock(transRec,
  205.                 (LOCKENTRY *) FIRST_LIST_ELEMENT( &(lockHeader->grantedList) )))    {
  206.  
  207.             return(NULL);
  208.         }
  209.  
  210.         /*
  211.          *    allocate a new entry to the request
  212.          */
  213.         if ((lockEntry = 
  214.             newLockEntry(transRec, lockHeader, WAIT_LIST, requestMode))==NULL){
  215.             return(NULL);
  216.         }
  217.  
  218.         /*
  219.          *    check to see if the transaction is privileged
  220.          */
  221.         if (transRec->privilege & TRANSPRIV_PREEMPT)    {
  222.  
  223.             TRPRINT(TR_LOCK, TR_LEVEL_1, ("TRANSPRIV_PREEMT"));
  224.             /*
  225.              *    put the request on the front of the list
  226.              */
  227.             listMovePush( &(lockHeader->waitList), &(lockEntry->headerList.list) );
  228.  
  229.             /*
  230.              *    blow the active transactions away
  231.              */
  232.             preemptLock(lockHeader, requestMode);
  233.         }
  234.         TRPRINT(TR_LOCK, TR_LEVEL_1, ("LOCK_TIMEOUT"));
  235.  
  236.         if( awaitLock(transRec->lock_timeout,
  237.             lockEntry, THREAD_LOCK_WAIT) == esmNOERROR) {
  238.             return lockEntry;
  239.         } else {
  240.             return NULL;
  241.         }
  242.     }
  243. }
  244.